package com.szc.fs.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.szc.fs.service.NewsInfoService;
import com.szc.fs.service.imp.NewsInfoServiceImp;
import com.szc.fs.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @title: JQRController
 * @Author Liuyang Tian
 * @Date: 2023/2/15 18:30
 * @Version 1.0
 */

@Controller
public class NewsInfoController {
    @Autowired
    private NewsInfoService newsInfoService;
    private static final Logger LOGGER = LoggerFactory.getLogger(NewsInfoController.class);

    @GetMapping("/fs/jqr/v1/pushmsg")
    @ResponseBody
    public String pushMsg() throws InvalidKeyException, NoSuchAlgorithmException {
        return newsInfoService.pushMsg();
    }
    @GetMapping({"/","/index.html"})
    public String pushMsg(Model model) throws InvalidKeyException, NoSuchAlgorithmException {
        return "index";
    }
    @PostMapping("/newsinfo/submit")
    @ResponseBody
    public Result submit(@RequestBody String requestBody, HttpServletRequest request) {
        LOGGER.info(request.getRemoteAddr() + "    "+ requestBody);
        if (requestBody == null || "".equals(requestBody)){
           return Result.fail("数据异常");
        }
        return  newsInfoService.submit(requestBody) ;
    }

    @PostMapping("/newsinfo/validator")
    @ResponseBody
    public JSONObject check(@RequestParam(value = "title",required = false)String title,@RequestParam(value = "url",required = false)String url) {
        return  newsInfoService.check(title,url);
    }



}
